package org.shangrila.bme.mb;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.model.SelectItem;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.event.RowEditEvent;
import org.shangrila.app.sys.cfg.AppInitInfo;
import org.shangrila.app.wfm.mb.WfmTaskMgrMB;
import org.shangrila.bme.eo.BmeReceipt;
import org.shangrila.bme.eo.BmeReceiptDetail;
import org.shangrila.bme.eo.BmeWfmService;
import org.shangrila.comm.dao.DbAccess;
import org.shangrila.wfm.eo.WfmTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named
@SessionScoped
public class BmeWfmPurchMB implements Serializable {
	private static final long serialVersionUID = -613146464217112L;
	private final Logger log = LoggerFactory.getLogger(this.getClass());

	@Inject
	private DbAccess db;

    private BmeReceipt io = new BmeReceipt();//录入    
	private List<BmeReceiptDetail> vod = new ArrayList<BmeReceiptDetail>(); //详情

	//获取相关数据字典
	private List<SelectItem> itemsPurchStatus = AppInitInfo.getDropdownList("PurchStatus"); 

	WfmTaskMgrMB wfmTaskMgrMB;
	private WfmTask selectedTask;	
	private String businessId="";

	@PostConstruct
    public void init() {
    }

	//接收WFM参数
	public void initwfm() {
		wfmTaskMgrMB = (WfmTaskMgrMB)AppInitInfo.findSessionBeanByName("wfmTaskMgrMB");
		//wfmTaskMgrMB = (WfmTaskMgrMB)FacesContext.getCurrentInstance().getApplication().getExpressionFactory().createValueExpression(FacesContext.getCurrentInstance().getELContext(),"#{wfmTaskMgrMB}", WfmTaskMgrMB.class).getValue(FacesContext.getCurrentInstance().getELContext());
		businessId = wfmTaskMgrMB.getSelectedTask().getServiceNo();
		log.info("initwfm="+businessId);
		//在BmeWfmService中根据serviceNo查找业务ID
		List<BmeWfmService> lbws = db.findByProperty("serviceNo", businessId, "BmeWfmService");
		if(lbws.size()>0) {;
			io = db.findById(BmeReceipt.class, lbws.get(0).getBusinessId());
			vod = io.getBmeReceiptDetails();
		}
	}

    public void onRowEdit(RowEditEvent event) {
    	BmeReceiptDetail selectedd = (BmeReceiptDetail) event.getObject();
    	selectedd.setSignOperator(wfmTaskMgrMB.getOperator());
    	db.beginTransaction();
    	db.update(selectedd);
    	db.commitTransaction();
    	AppInitInfo.showMessage("签收数量修改", "处理成功");
    }   
    
    public void onRowCancel(RowEditEvent event) {
    	AppInitInfo.showMessage("取消修改", "处理成功");
    }
    
	public String nullAction() {
		return null;
	}
	
	public BmeReceipt getIo() {
		return io;
	}

	public void setIo(BmeReceipt io) {
		this.io = io;
	}

	public List<BmeReceiptDetail> getVod() {
		return vod;
	}

	public void setVod(List<BmeReceiptDetail> vod) {
		this.vod = vod;
	}

	public WfmTask getSelectedTask() {
		return selectedTask;
	}

	public void setSelectedTask(WfmTask selectedTask) {
		this.selectedTask = selectedTask;
	}

	public List<SelectItem> getItemsPurchStatus() {
		return itemsPurchStatus;
	}

	public void setItemsPurchStatus(List<SelectItem> itemsPurchStatus) {
		this.itemsPurchStatus = itemsPurchStatus;
	}

	public String getBusinessId() {
		return businessId;
	}

	public void setBusinessId(String businessId) {
		this.businessId = businessId;
	}

}